﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WordRacer.Domain.Model.Boards;
using WordRacer.Domain.Model.Dictionaries;

namespace WordRacer.Domain.Tests.Unit.Builders
{
  public class BoardBuilder2x2
  {
    private char topLeftLetter { get; set; }
    private char topRightLetter { get; set; }
    private char bottomLeftLetter { get; set; }
    private char bottomRightLetter { get; set; }
    private GameDictionary _gameDictionary { get; set; }
    private bool GameDictionaryProvided { get; set; }

    /// <summary>
    ///  + --- + --- +
    ///  |  H  |  I  | 
    ///  + --- + --- + 
    ///  |  G  |  T  |
    ///  + --- + --- + 
    ///  Letters are default unless otherwise specified.
    ///  All cells connect to each other, so 4 letter words are possible.
    ///  Default Dictionary Words: HI, IT, GIT, TIG, HIT
    /// </summary>
    public BoardBuilder2x2()
    {
      GameDictionaryProvided = false;
      topLeftLetter = 'H';
      topRightLetter = 'I';
      bottomLeftLetter = 'G';
      bottomRightLetter = 'T';
    }

    public Board Build()
    {
      var boardFactory = new BoardFactory();
      if (!GameDictionaryProvided)
      {
        _gameDictionary = BuildDefaultGameDictionary();
      }
      var testBoard = boardFactory.CreateGameBoard(_gameDictionary);

      var gameCellBuilder = new CellBuilder();
      
      //add top left
      var topLeftCell = gameCellBuilder.withLetter(topLeftLetter).Build();
      //add top right
      var topRightCell = gameCellBuilder.withLetter(topRightLetter).Build();
      //add bottom left
      var bottomLeftCell = gameCellBuilder.withLetter(bottomLeftLetter).Build();
      //add bottom right
      var bottomRightCell = gameCellBuilder.withLetter(bottomRightLetter).Build();
      //connect all cells

      testBoard.AddGameCell(topLeftCell);
      testBoard.AddGameCell(topRightCell);
      testBoard.AddGameCell(bottomLeftCell);
      testBoard.AddGameCell(bottomRightCell);

      testBoard.ConnectTwoCells(topLeftCell, topRightCell);
      testBoard.ConnectTwoCells(topLeftCell, bottomLeftCell);
      testBoard.ConnectTwoCells(topLeftCell, bottomRightCell);
      testBoard.ConnectTwoCells(topRightCell, bottomLeftCell);
      testBoard.ConnectTwoCells(topRightCell, bottomRightCell);
      testBoard.ConnectTwoCells(bottomLeftCell, bottomRightCell);

      return testBoard;
    }

    public BoardBuilder2x2 WithTopLeftLetter(char character)
    {
      topLeftLetter = character;
      return this;
    }
    public BoardBuilder2x2 WithTopRightLetter(char character)
    {
      topRightLetter = character;
      return this;
    }
    public BoardBuilder2x2 WithBottomLeftLetter(char character)
    {
      bottomLeftLetter = character;
      return this;
    }
    public BoardBuilder2x2 WithBottomRightLetter(char character)
    {
      bottomRightLetter = character;
      return this;
    }

    public BoardBuilder2x2 WithDictionary(GameDictionary gameDictionary)
    {
      _gameDictionary = gameDictionary;
      GameDictionaryProvided = true;
      return this;
    }

    private GameDictionary BuildDefaultGameDictionary()
    {
      var dictionaryBuilder = new DictionaryBuilder();
      var gameDictionary = dictionaryBuilder.withWord("HI").withWord("IT").withWord("GIT").withWord("TIG").withWord("HIT").Build();
      return gameDictionary;
    }


  }
}
